package gov.pbc.nn.kjgl.train.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.kjgl.entity.TrainingPlan;
import gov.pbc.nn.kjgl.entity.TrainingScore;
import gov.pbc.nn.kjgl.entity.TrainingUser;
import gov.pbc.nn.kjgl.train.dao.TrainPlanDao;
import gov.pbc.nn.kjgl.train.dao.TrainUserDao;

@Component
public class TrainUserPR {

	@Autowired
	private TrainUserDao trainUserDao;

	@Autowired
	private TrainPlanDao trainPlanDao;

	@DataProvider
	public void getAll(Page<TrainingUser> page,Criteria criteria,String id) {
		trainUserDao.getAll(page,criteria, id);
	}

	@DataProvider
	public void getAllById(Page<TrainingUser> page, String id) {
		trainUserDao.getAllById(page, id);
	}

	@DataProvider
	public TrainingScore getAllScor(String username,String busKey) {
		return trainUserDao.getAllScor(username,busKey);
	}

	@Transactional
	@DataResolver
	public void save(List<TrainingUser> list) {
		trainUserDao.save(list);
	}

	@Transactional
	@DataResolver
	public void saveOther(List<TrainingUser> list, String id) {
		trainUserDao.saveOther(list, id);
	}

	/**
	 * 删除参会人员，然后统计人数
	 * @param ids
	 */
	@Expose
	@Transactional
	public void delete(List<String> ids) {
		for (String Id : ids) {
			// Id 为TrainingUser主键，id 为 TrainingUser的busKey
			//1，根据Id 得到业务表的主键，然后进行删除人员操作。
			//2，根据 业务部id 得到主表数据，然后，将查询出来的人数进行修改
			String id = this.trainUserDao.getTrainingUser(Id).getBusKey();//得到业务表buskey
			String username = this.trainUserDao.getTrainingUser(Id).getUsername();//得到业务表usernmae
			List<TrainingScore> score = getTrainingScoreByUsername(id,username);//根据username 和 projectId 得到分数 然后删除
			if (score != null) {
				for (TrainingScore m : score) {
					this.trainUserDao.delete(m);
				}
			}
			trainUserDao.delete(Id);
			TrainingPlan trainPlan = trainPlanDao.getTrainPlanById(id);//得到主表数据
			int count = trainPlanDao.getCountUser(id);//查询的人数
			trainPlan.setPeoples(count);
			this.trainPlanDao.update(trainPlan);
		}
	}
	
	public List<TrainingScore> getTrainingScoreByUsername(String id,String username) {
		DetachedCriteria query = DetachedCriteria.forClass(TrainingScore.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("projectId", id));
		query.add(Restrictions.eq("usreName", username));
		List<TrainingScore> ls = (List<TrainingScore>) this.trainUserDao.query(query);
		if (ls.size() > 0) {
			return ls;
		}
		return null;
	}
}
